import { defineStore } from "pinia";
import dayjs from "dayjs";

/**
 * 应用程序类型定义
 * @property label - 应用程序的标签名称
 */
export type AppsType = {
    label: string;
    icon: string;
    isHome?: boolean;
    mode: "SCAN_CODE" | "LINK";
    url: string;
    operationTime: number;
};

type AppItemType = {
    label: string;
    apps: AppsType[];
};

/**
 * 应用程序存储状态类型定义
 * @property applications - 应用程序列表
 */
type AppsStoreType = {
    applications: AppItemType;
};

export const useAppsStore = defineStore("apps", {
    /**
     * 初始化存储状态
     * @returns 返回初始状态对象，包含空的应用程序数组
     */
    state: (): AppsStoreType => ({
        applications: {
            label: "",
            apps: [
                {
                    label: "记账助手",
                    isHome: true,
                    icon: "https://assets.iclss.com/images/app/bill-record-app-icon.svg",
                    url: "",
                    mode: "LINK",
                    operationTime: 0
                },
                {
                    label: "体重管理",
                    isHome: true,
                    icon: "https://assets.iclss.com/images/app/weight-app-icon.svg",
                    url: "",
                    mode: "LINK",
                    operationTime: 0
                },
                {
                    label: "提醒事项",
                    isHome: true,
                    icon: "https://assets.iclss.com/images/app/remind-app-icon.svg",
                    url: "",
                    mode: "LINK",
                    operationTime: 0
                },
                {
                    label: "档案管理",
                    isHome: false,
                    icon: "https://assets.iclss.com/images/app/id-app-icon.svg",
                    url: "",
                    mode: "LINK",
                    operationTime: 0
                },
                {
                    label: "记录",
                    icon: "https://assets.iclss.com/images/app/record-app-icon.svg",
                    url: "",
                    mode: "LINK",
                    operationTime: 0
                },
                {
                    label: "健康监督",
                    icon: "https://assets.iclss.com/images/app/remind-app-icon.svg",
                    url: "",
                    mode: "LINK",
                    operationTime: 0
                },
                {
                    label: "周期追踪",
                    icon: "https://assets.iclss.com/images/app/remind-app-icon.svg",
                    url: "",
                    mode: "LINK",
                    operationTime: 0
                }
            ]
        }
    }),
    actions: {
        /**
         * 添加首页程序，isHome 设置为 true 并且移动到最后一位
         * @param app - 要添加的应用程序对象
         */
        updateHomeApp(app: AppsType) {
            if (this.applications.apps.filter((item) => item.isHome).length < 7) {
                const index = this.applications.apps.findIndex((item) => item.label === app.label);
                this.applications.apps[index].isHome = true;
                this.applications.apps[index].operationTime = dayjs().valueOf();
            }
        },
        /**
         * 删除应用程序
         * @param app - 要删除的应用程序对象
         */
        removeHomeApp(app: AppsType) {
            const index = this.applications.apps.findIndex((item) => item.label === app.label);

            this.applications.apps[index].isHome = false;
        }
    },
    getters: {
        homeApplications(state) {
            return state.applications.apps.filter((item) => item.isHome);
        },
        homeExceptApplications(state) {
            return state.applications.apps.filter((item) => !item.isHome);
        }
    },
    /**
     * 配置数据持久化策略
     * 启用数据持久化功能，根据当前运行模式生成唯一的存储键名
     */
    persist: {
        enabled: true,
        strategies: [
            {
                key: import.meta.env.MODE + "-application-store"
            }
        ]
    }
});
